package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Main;
import niumao.minecraft.uhc.Util;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

public class goldenHead_Cornucopia implements Listener {
    private static final Main plugin = Main.getPlugin(Main.class);

    //吃head判断
    @EventHandler
    public void on(PlayerInteractEvent event) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                ItemMeta itemMeta = event.getPlayer().getInventory().getItemInOffHand().getItemMeta();
                if (itemMeta == null) return;
                if (itemMeta.getDisplayName().equals(Util.getStr("GoldenHead")))
                    event.setCancelled(true);
            }
            return;
        }
        Player player = event.getPlayer();
        ItemStack item = player.getInventory().getItemInMainHand();
        ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta == null) return;
        if (event.getHand() == EquipmentSlot.HAND) {
            if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && itemMeta.getDisplayName().equals(Util.getStr("GoldenHead"))) {
                if (item.getAmount() == 1) {
                    player.getInventory().setItem(player.getInventory().getHeldItemSlot(), null);
                } else {
                    item.setAmount(item.getAmount() - 1);
                }
                applyEffect(player,true);
                player.sendMessage(plugin.getConfig().getStringList("item." + Main.getLang() + ".GoldenHead.ateMessage").get(0));
                player.sendMessage(plugin.getConfig().getStringList("item." + Main.getLang() + ".GoldenHead.ateMessage").get(1));
                event.setCancelled(true);
            }
        }

    }

    //吃Cornucopia判断
    @EventHandler
    public void on2(PlayerInteractEvent event) {
        if (event.getHand() == EquipmentSlot.OFF_HAND) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                ItemMeta itemMeta = event.getPlayer().getInventory().getItemInOffHand().getItemMeta();
                if (itemMeta == null) return;
                if (itemMeta.getDisplayName().equals(Util.getStr("Cornucopia")))
                    event.setCancelled(true);
            }
            return;
        }
        Player player = event.getPlayer();
        ItemStack item = player.getInventory().getItemInMainHand();
        ItemMeta itemMeta = item.getItemMeta();
        if (itemMeta == null) return;
        if (event.getHand() == EquipmentSlot.HAND) {
            if ((event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
                    && itemMeta.getDisplayName().equals(Util.getStr("Cornucopia"))) {
                if (item.getAmount() == 1) {
                    player.getInventory().setItem(player.getInventory().getHeldItemSlot(), null);
                } else {
                    item.setAmount(item.getAmount() - 1);
                }
                applyEffect(player,false);
                player.sendMessage(plugin.getConfig().getString("item." + Main.getLang() + ".Cornucopia.ateMessage"));
                event.setCancelled(true);
            }
        }

    }

    private void applyEffect(Player player, boolean isHead) {
        if (isHead) {
            player.removePotionEffect(PotionEffectType.ABSORPTION);
            player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 120 * 20, 0));

            player.removePotionEffect(PotionEffectType.REGENERATION);
            player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 5 * 20, 2));

            player.removePotionEffect(PotionEffectType.SPEED);
            player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 15 * 20, 1));
            player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP, 1, 1.5f);
            return;
        }
        player.removePotionEffect(PotionEffectType.SATURATION);
        player.addPotionEffect(new PotionEffect(PotionEffectType.SATURATION, 10 * 60 * 20, 0));

        player.removePotionEffect(PotionEffectType.REGENERATION);
        player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 10 * 20, 1));
        player.playSound(player.getLocation(), Sound.ENTITY_PLAYER_BURP, 1, 1.5f);
    }
}